#include "Service.h"
#include "JSON.h"

Service::Service(IArduino& arduino, MessageRouter& router, const String& name, ILogger& logger) : arduino_(arduino), router_(router), logger_(logger) {
	name_ = allocateString(name);
}
Service::~Service(){
	free(name_);
}
bool Service::operator==(const Service& s) const 
{
	return getName() == s.getName();
}
void Service::postMessage(const String& msg){
	router_.postMessage(*this, msg);
}
void Service::onMessage(const String& msg) {
	JsonParser p(*this);
	p.parse(msg);
}
void Service::tick(long milliseconds) {
}
bool Service::onItem(const String& name, const String& value) {
	return false;
}
void Service::onBegin() {}
void Service::onEnd() {}	
void Service::onParseError() {}
const char* Service::getName() const{
	return name_;
}
bool Service::isInfrastructure() const{
	return false;
}
char* Service::allocateString(const String& str){
	int len = str.length();
	char* ret = (char*)malloc(len+1);
	for(int i = 0; i < len; ++i){
		ret[i] = str[i];
	}
	ret[len]=0;
	return ret;
}
